package ProR;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;

public class Login extends JFrame implements ActionListener{
	private static final long serialVersionUID = 1L;
	private Font f;
	private JPanel p;
	private JLabel labelHeader,labelName,labelPassword,labelPic;
	private JTextField textName;
	private JPasswordField textPassword;
	private JButton buttonLogin;
	static int privilege;
         
    public Login(){
    	super("Login");
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	setLayout(null);
    	
    	p = new JPanel(null);
     	p.setBorder(BorderFactory.createEtchedBorder());
    	setComponent();
    	
    	p.setBounds(8,5,332,195);
        p.setVisible(true);
        setBounds(455,160,352,232);
        add(p);
        
        setResizable(false);

    }
    
    public void setComponent(){
       
        f = new Font("Tahoma",Font.PLAIN,12);
               
         //Create
        labelHeader = new JLabel("ใส่ชื่อและรหัสพนักงานเพื่อเข้าสู่ระบบ..");   
        labelName = new JLabel("ชื่อ:");       
        labelPassword = new JLabel("รหัสผ่าน:");               
        labelPic = new JLabel(" ");            
        textName = new JTextField();           
        textPassword = new JPasswordField();                       
        buttonLogin = new JButton("LOGIN");
        
        //setFont
        labelHeader.setFont(new Font("Tahoma",Font.PLAIN,11));
        labelHeader.setFont(f);
        labelName.setFont(f);
        labelPassword.setFont(f);
        textName.setFont(f);
       	textPassword.setFont(f);       
        buttonLogin.setFont(f);
        
        //add
        p.add(labelHeader);
        p.add(labelName);
        p.add(labelPassword);
        p.add(labelPic);         
        p.add(textName);
        p.add(textPassword);             
        p.add(buttonLogin);
        
        //setIcon
        labelPic.setIcon(new javax.swing.ImageIcon("D:\\Project\\src\\pic\\Login.png"));             
        
        //setBounds
        labelHeader.setBounds(20, 45, 300, 20);
        labelName.setBounds(90, 75, 90, 20);
        labelPassword.setBounds(65,100, 90, 20);
        textName.setBounds(120, 75, 140, 20);
        textPassword.setBounds(120, 105, 140, 20);
        buttonLogin.setBounds(120, 145, 70,25);
        labelPic.setBounds(10, 10, 150,50);
        
        
        //action of button Login
        buttonLogin.addActionListener(this);
        textName.addActionListener(this);
        textPassword.addActionListener(this);
	
    }
    
    public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		/*if(textName.getText().equals("")||textPassword.getText().equals("")){
			JOptionPane.showMessageDialog(null, "กรุณาใส่ชื่อและรหัสให้ครบก่อนครับ...","WARNING_MESSAGE",JOptionPane.WARNING_MESSAGE);
		}
		else{
			try{
				Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
				Connection con = DriverManager.getConnection("jdbc:odbc:MSL");
				Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
				ResultSet re = stmt.executeQuery("SELECT * FROM MSLM_MAIN_USER_LOGIN WHERE '"+textName.getText()+"' = LOGIN_NAME AND '"+textPassword.getText()+"' = LOGIN_PASSWORD");
				Statement stmt2 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
				ResultSet re2 = stmt2.executeQuery("SELECT * FROM MSLM_MAIN_USER_LOGIN left join MSLT_MAIN_USER_PRIVILEGE on MSLM_MAIN_USER_LOGIN.LOGIN_ID = MSLT_MAIN_USER_PRIVILEGE.STAMP_ID where '"+textName.getText()+"' = LOGIN_NAME");
				re.first();
				if(re.getRow() == 0)
					JOptionPane.showMessageDialog(null, "ชื่อหรือรหัสไม่ถูกต้อง...", "MESSAGE_ERROR", JOptionPane.ERROR_MESSAGE);
				else{
					re2.first();
					String state = re2.getString("STAMP_PRIVILEGE");
					if(state.equals("t"))*/
						setPrivilege(0);
					/*else
						setPrivilege(1);*/
					dispose(); //windows login close
					//call Main Windows
				
					new Main();
				/*}
				re.close();
				re2.close();
				con.close();
			}
			catch(SQLException s){
				JOptionPane.showMessageDialog(null, "SQL_ERROR", "MESSAGE_ERROR", JOptionPane.ERROR_MESSAGE);
			}
			catch(ClassNotFoundException c){
				JOptionPane.showMessageDialog(null, "CLASS_NOTFOUND", "MESSAGE_ERROR", JOptionPane.ERROR_MESSAGE);
			}
		}//end else
*/	}//end action performed
    
    public static void setPrivilege(int pi){
    	privilege = pi;
    }
    
    public static int getPrivilege(){
    	return privilege;
    }
    
    public static void main(String[] args){
    	Login log = new Login();
    	log.setVisible(true);
    }
 }


